//
//  RegisterViewController.m
//  HuLaQuan
//
//  Created by hok on 1/7/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "RegisterViewController.h"

//Views
#import "YuLoginInputInfoField.h"


//Header
#import "LoginRelatedHeader.h"

#import "YuLoginRegisterUtilities.h"

//
#import "RegisterFinishViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet YuLoginInputInfoField *secretField;
@property (weak, nonatomic) IBOutlet YuLoginInputInfoField *nameField;

@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@end
@implementation RegisterViewController
- (void)setupUI
{
    self.title = @"快速注册";
    // 加手势处理键盘
    [self setupTapGesture];
    
    //自定义navigationbar返回按钮
    [self setupNavBackButton];
    
    //文本框没有内容时，按钮处于不能点击状态
    self.nextButton.enabled = NO;
    
    //处理文本框的提示文字
    [self setupTextField];
}
//处理文本框的提示文字
- (void)setupTextField
{
    [self.nameField setColorfulPlaceholder:kRUsernameFieldPlaceholder];
    [self.secretField setColorfulPlaceholder:kRPasswordFieldPlaceholder];
}

// 加手势处理键盘
- (void)setupTapGesture
{
    UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelGenericTapView:)];
    tapRec.delegate = self;
    [self.view addGestureRecognizer:tapRec];
}
- (void)handelGenericTapView:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        // handling code
        // [self.view endEditing:YES];
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
    }
}

//注册检测
- (IBAction)nextButton:(id)sender {
    if (self.nameField.text) {
       // [self performSegueWithIdentifier:@"ShowRegisterFinishViewController" sender:self];
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        [self.navigationController pushViewController:[storyBoard instantiateViewControllerWithIdentifier:@"RegisterFinishViewController"] animated:YES];
       // NSLog(@"%@",)
    }
}
#pragma mark - Controller Life Cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginFinish:) name:@"LoginFinish" object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
   // [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LoginFinish" object:nil];
   // [self.timerForVeirify invalidate];
    [super viewWillDisappear:YES];
    
}


/*
 处理及判断文本框输入内容
 */
#pragma mark - Textfield delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    YuLoginInputInfoField *textF = (YuLoginInputInfoField *)textField;
    [textF setColorfulPlaceholder:@""];
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length == 0 ) {
        YuLoginInputInfoField *textF = (YuLoginInputInfoField *)textField;
        [textF setColorfulPlaceholder:([textField isEqual:self.nameField] ? kUsernameFieldPlaceholder : kPasswordFieldPlaceholder)];
        
    }
    if (self.nameField.text.length == 0 || self.secretField.text.length == 0) {
        self.nextButton.enabled = NO;
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (self.nameField.text.length > 0 && self.secretField.text.length > 0) {
        self.nextButton.enabled = YES;
    }
    //检测密码长度
    ////NSLog(@"%@",string);
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (self.secretField == textField)
    {
        if ([toBeString length] > kPasswordLength) {
            textField.text = [toBeString substringToIndex:kPasswordLength];
            return NO;
        }
    }
    
    return YES;
    // return [self validateNumber:string];
}



 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
     
     if ([segue.identifier isEqualToString:@"ShowRegisterFinishViewController"]) {
         RegisterFinishViewController *con = (RegisterFinishViewController *)[segue destinationViewController];
         
         con.registerInfoDic = [@{@"name":self.nameField.text,@"password":self.secretField.text} mutableCopy];
         
     }
 }

@end
